Baraj selectie lot olimpic, Bucuresti, mai 1997
Problema 4, Triplete "Steiner" (Ioan Maxim)
Dificultate: C4

	Un sistem "Steiner" de triplete este un aranjament de n numere organizate 
in triplete, astfel incat din acestea se obtin toate perechile neordonate ce se 
pot forma cu cele n numere, o singura data fiecare pereche. 
	Pentru un numar dat n, sa se construiasca un sistem de triplete "Steiner".

Nota: Dintr-o tripleta se pot obtine 3 perechi. O pereche trebuie sa se obtina 
numai dintr-o singura tripleta.

Date de intrare:
	Fisierul de intrare "n.txt" contine pe o linie numarul n (3<=n<=100).
Date de iesire:
	Fisierul de iesire "steiner.txt" va contine:
k		- numarul maxim de triplete Steiner care se pot forma cu cele n 
	numere
a11 a12 a13 	- tripleta 1
a21 a22 a23 	- tripleta 2	daca exista un sistem de triplete
...............
ak1 ak2 ak3 	- tripleta k        sau 
0				daca nu exista nici un sistem de triplete.

Exemple:
	Pentru fisierul de intrare "n.txt" ce contine:
7
	Fisierul de iesire "steiner.txt" poate contine:
7
1 2 4
2 3 5
3 4 6
4 5 7
5 6 1
6 7 2
7 1 3

Pentru fisierul de intrare "n.txt" ce contine:
4
Fisierul de iesire "steiner.txt" va contine:
0

Timp maxim de executare/test: 20 secunde
=================================

	TESTE
Teste:
test 1:
19
-------------------
test 2:
20
-----------------
test 3:
51
--------------
test 4:
99
------------
test 5:
100
===================
	Program de evaluare a problemei Steiner
var a:array[1..1650,1..3] of byte;
    g,f,fr:text;
    n,k,i,j,x,puncte,punct:word;
    t:boolean;
    nume1,nume2:string[10];
begin
   assign(fr,'rez.txt'); rewrite(fr);
   punct:=0;
   for x:=1 to 5 do
       begin
          nume1:='rc'+chr((ord(x)+ord('0')));
          nume2:='t'+chr((ord(x)+ord('0')));
          assign(f,nume1);reset(f);
          assign(g,nume2); reset(g);
          writeln('Lucrez... la testul ',x);
   reset(g);
   readln(g,n);
   close(g);
   puncte:=0;
   reset(f);
   t:=true;
   if seekeof(f) then
      begin
         writeln(' Eroare. Fisier vid. ');
{         halt;}
      end
   else
      begin
         readln(f,k);
         if (((n*n-n-6) mod 12 <>0) or ((n*n-n) mod 6 <> 0)) and (k=0) then
            begin
               writeln(' Corect. Nu exista sistem de triplete. ');
               t:=false;
               puncte:=2;
            end
         else
            if (((n*n-n-6) mod 12 = 0) and ((n*n-n) mod 6 = 0)) and (k=(n*(n-1)) div 6) then
               begin
                  writeln(' Numar triplete corect. ');
                  puncte:=1;
                  i:=0;
                  while not seekeof(f) do
                     begin
                        readln(f);
                        i:=i+1;
                     end;
                  if i<>k then
                     begin
                        writeln(' Eroare. Numar triplete generate:',i,' incorect. ');
                {        halt;}
                     end
                  else
                     begin
                        close(f);
                        reset(f);
                        readln(f);
                        t:=false;
                        for i:=1 to k do
                            begin
                               readln(f,a[i,1],a[i,2],a[i,3]);
                               if (a[i,1]>n) or (a[i,2]>n) or (a[i,3]>n) then
                                  t:=true;
                            end;
                        if not t then
                           begin
                              i:=1;
                              repeat
                                 j:=i+1;
                                 while (j<=n) and not t do
                                    begin
                                       if (a[i,1]=a[j,1]) and (a[i,2]=a[j,2]) or
                                          (a[i,1]=a[j,1]) and (a[i,2]=a[j,3]) or
                                          (a[i,1]=a[j,2]) and (a[i,2]=a[j,3]) then
                                          t:=true;
                                       if (a[i,1]=a[j,1]) and (a[i,3]=a[j,2]) or
                                          (a[i,1]=a[j,1]) and (a[i,3]=a[j,3]) or
                                          (a[i,1]=a[j,2]) and (a[i,3]=a[j,3]) then
                                          t:=true;
                                       if (a[i,2]=a[j,1]) and (a[i,3]=a[j,2]) or
                                          (a[i,2]=a[j,1]) and (a[i,3]=a[j,3]) or
                                          (a[i,2]=a[j,2]) and (a[i,3]=a[j,3]) then
                                          t:=true;
                                       j:=j+1;
                                    end;
                                 i:=i+1;
                              until (i=n) or t;
                              if not t then
                                 begin
                                    writeln(' Corect.');
                                    puncte:=2;
                                 end
                              else
                                 begin
                                    write(' Eroare. Exista perechi egale. ');
                                    writeln(a[i,1],' ',a[i,2],' ',a[i,3],'-',a[j,1],' ',a[j,2],' ',a[j,3]);
                                 end;
                           end
                        else
                           writeln(' Elemente triplet > ',n);
                     end;
               end
            else
               if ((n*n-n-6 mod 12 <> 0) or (n*n-n mod 6 <> 0)) and (k=(n*n-n) div 6) then
                  begin
                     writeln(' Eroare. Nu exista sistem Steiner');
              {       halt;}
                  end;
         close(f);
         write(' Press <Enter> ');
         readln;
      end;
      case x of
           1,2: begin
                 writeln(fr,'Testul ',x,':');
                 if puncte=1 then
                    puncte:=2
                 else
                    if puncte=2 then
                       puncte:=5;
                 writeln(fr,'Din 5 puncte s-au obtinut ',puncte,'!');
                 punct:=punct+puncte;
              end;
           3: begin
                 writeln(fr,'Testul ',x,':');
                 if puncte=1 then
                    puncte:=5
                 else
                    if puncte=2 then
                       puncte:=10;
                 writeln(fr,'Din 10 puncte s-au obtinut ',puncte,'!');
                 punct:=punct+puncte;
              end;
           4,5: begin
                 writeln(fr,'Testul ',x,':');
                 if puncte=1 then
                    puncte:=7
                 else
                    if puncte=2 then
                       puncte:=15;
                 writeln(fr,'Din 15 puncte s-au obtinut ',puncte,'!');
                 punct:=punct+puncte;
              end;
      end;
              end;
      writeln(fr,'SCORUL OBTINUT: ',punct,' puncte');
      close(fr);
end.
================================
